<?php

namespace Model\QuestionBundle\Controller;

use Model\CourseCategoryBundle\Entity\CourseCategory;
use Model\CourseCategoryBundle\Model\CourseCategoryModel;
use Model\QuestionBundle\Entity\Attach;
use Model\QuestionBundle\Entity\Choice;
use Model\QuestionBundle\Entity\ChoiceStatic;
use Model\QuestionBundle\Entity\Question;
use Model\QuestionBundle\Entity\QuestionStatic;
use Model\QuestionBundle\Setting\QuestionSetting;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use Base\BaseController;
use Model\QuestionBundle\Entity\Question as Entity;

/**
 * @route("/admin/")
 */
class QuestionAdminController extends BaseController
{

    /**
     * @route("Question/add",name="QuestionPreAdd")
     */
    public function add(Request $request)
    {
        if ($request->getMethod() == "POST") {
            if (!$type = $this->getParamRequired("type")) {
                if ($type !== "0") {
                    return $this->msg_out("请选择题型");
                }
            }
            if (!$title = $this->getParamRequired("title")) {
                return $this->msg_out("请填写题干");
            }
            if (!$Point = $this->getParamRequired("Point")) {
                return $this->msg_out("请设置知识点");
            }
            if (!$difficulty = $this->getParamRequired("difficulty")) {
                if ($difficulty !== "0") {
                    return $this->msg_out("请设置难度系数");
                }
            }
            if (!$choice = $this->getParamRequired("choice")) {
                return $this->msg_out("您未设置选项");
            }
            if (!$choiceIsAnswer = $this->getParamRequired("choiceIsAnswer")) {
                return $this->msg_out("您未设置答案");
            }
            if (!is_array($choiceIsAnswer)) {
                return $this->msg_out("您未设置答案");
            }
            if (!$AnswerAnalyze = $this->getParamRequired("AnswerAnalyze")) {
                return $this->msg_out("请填写答案解析");
            }
            if (!$AttachType = $this->getParamRequired("AttachType")) {
                $AttachType = 0;
            }
            if (!$AttachData = $this->getParamRequired("AttachData")) {
                $AttachData = 0;
            }

            $em = $this->getDoctrine()->getManager();

            if ($AttachType) {
                $em = $this->getDoctrine()->getManager();
                //创建附件对象
                $Attach = new Attach();
                $Attach->setType($AttachType);
                $Attach->setData($AttachData);
                $Attach->setUserId(0);
                $em->persist($Attach);
                $em->flush();
                $AttachId = $Attach->getId();
            }else{
                $AttachId = 0;
            }

            //创建题目对象
            $Question = new Question();
            $Question->setType($type);
            $Question->setTitle($title);
            $Question->setUserId(0);
            $Question->setSort($Point);
            $Question->setDifficulty($difficulty);
            $Question->setAnswerAnalyze($AnswerAnalyze);
            $Question->setAttachId($AttachId);

            $i = 0;
            $answer = "";
            foreach ($choice as $key => $item) {
                if (array_key_exists($key,$choiceIsAnswer)) {
                    $answer .= "[$i]";
                }
                $i++;
            }
            $Question->setAnswer($answer);

            $em->persist($Question);
            $em->flush();

            $QuestionId = $Question->getId();

            //创建题目统计对象
            $QuestionStatic = new QuestionStatic();
            $QuestionStatic->setQuestionId($QuestionId);
            $em->persist($QuestionStatic);
            $em->flush();

            //创建选项对象
            foreach ($choice as $key => $item) {
                $Choices = new Choice();
                $Choices->setQuestionId($QuestionId);
                $Choices->setContent($item);
                $Choices->setIsAnswer( array_key_exists($key,$choiceIsAnswer) ? 1 : 0);
                $em->persist($Choices);
                $em->flush();
                $ChoiceId = $Choices->getId();
                $ChoiceStatic = new ChoiceStatic();
                $ChoiceStatic->setQuestionId($QuestionId);
                $ChoiceStatic->setChoiceId($ChoiceId);
                $em->persist($ChoiceStatic);
                $em->flush();
            }

            return $this->lists();
        } else {
            $CourseCategory = new CourseCategoryModel($this->getDoctrine());
            $groups = $CourseCategory->getAllGroupByParent();

            $data = array(
                "sorts" => json_encode($groups),
            );
            return $this->render("ModelQuestionBundle:Question:add_advance.php.twig",$data);
        }
    }



    /**
     * @route("Question/Sort/{id}",name="QuestionSort")
     *
     * @Method({"GET", "POST"})
     */
    public function QuestionSort(Request $request , Question $question)
    {
        if ($request->getMethod() == "POST") {
            if (!$Point = $this->getParamRequired("Point")) {
                return $this->msg_out("请设置知识点");
            }

            $em = $this->getDoctrine()->getManager();
            $question->setSort($Point);
            $em->persist($question);
            $em->flush();

            return $this->lists();
        } else {
            $CourseCategory = new CourseCategoryModel($this->getDoctrine());
            $groups = $CourseCategory->getAllGroupByParent();
            $rep = $this->getDoctrine()->getRepository("ModelCourseCategoryBundle:CourseCategory");
            $Sort = $rep->find($question->getSort());
            $link = $Sort->getLink();
            $link = str_replace("]","",$link);
            $linkArr = explode("[",substr($link,1));

            $data = array(
                "sorts" => json_encode($groups),
                "Question" => $question,
                "Sort" => json_encode($linkArr),
            );
            return $this->render("ModelQuestionBundle:Question:edit_point.php.twig",$data);
        }
    }

    /**
     * @route("Question/Choice/{id}",name="QuestionChoice")
     *
     * @Method({"GET", "POST"})
     */
    public function QuestionChoice(Request $request , Question $question)
    {
        $rep = $this->getDoctrine()->getRepository("ModelQuestionBundle:Choice");
        $em = $this->getDoctrine()->getManager();
        if ($request->getMethod() == "POST") {
            if (!$choices = $this->getParamRequired("choices")) {
                return $this->msg_out("选项不能为空");
            }
            if (!$choiceIsAnswer = $this->getParamRequired("choiceIsAnswer")) {
                return $this->msg_out("至少设置一个正确答案");
            }
            foreach ($choices as $id => $content) {
                $c = $rep->find($id);
                $c->setContent($content);
                $em->persist($c);
                $em->flush();
            }
            return $this->lists();
        } else {
            $Choices = $rep->findBy(array("questionId"=>$question->getId()));
            $data = array(
                "Choices" => $Choices,
                "Question" => $question,
            );
            return $this->render("ModelQuestionBundle:Question:edit_choice.php.twig",$data);
        }
    }

    /**
     * @route("Question/Attach/{id}",name="QuestionAttach")
     *
     * @Method({"GET", "POST"})
     */
    public function QuestionAttach(Request $request , Question $question)
    {
        $rep = $this->getDoctrine()->getRepository("ModelQuestionBundle:Attach");
        $em = $this->getDoctrine()->getManager();
        if ($request->getMethod() == "POST") {
            if (!$id = $this->getParamRequired("id")) {
                return $this->msg_out("数据错误，请刷新后重试");
            }
            if (!$AttachType = $this->getParamRequired("AttachType")) {
                $AttachType = 0;
            }
            if (!$AttachData = $this->getParamRequired("AttachData")) {
                $AttachType = 0;
            }
            $c = $rep->find($id);
            $c->setType($AttachType);
            $c->setData($AttachData);
            $em->persist($c);
            $em->flush();
            return $this->lists();
        } else {
            $Attach = $rep->find($question->getAttachId());
            $data = array(
                "Attach" => $Attach,
                "Question" => $question,
            );
            return $this->render("ModelQuestionBundle:Question:edit_attach.php.twig",$data);
        }
    }


    /**
     * @route("Question/add/do",name="QuestionDoAdd")
     *
     * @Method({"GET", "POST"})
     */
    public function doAdd(Request $request)
    {
        //私有代码
        $entity = new Entity();

        //通用代码
        $this->setSetting(new QuestionSetting($this->getDoctrine()->getManager()));
        return $this->comDoAdd($request,$entity);
    }

    /**
     * @route("Question/edit/{id}",name="QuestionPreEdit")
     */
    public function edit(Entity $entity)
    {
        $this->setSetting(new QuestionSetting($this->getDoctrine()->getManager()));
        return $this->comEdit($entity);
    }

    /**
     * @route("Question/edit/{id}/do",name="QuestionDoEdit")
     *
     * @Method({"GET", "POST"})
     */
    public function doEdit(Request $request,Entity $entity)
    {
        $this->setSetting(new QuestionSetting($this->getDoctrine()->getManager()));
        return $this->comDoEdit($request,$entity);
    }

    /**
     * @route("Question",name="QuestionLists")
     */
    public function lists()
    {
        //私有部分
        $tools = array();

        //通用部分·
        $this->setSetting(new QuestionSetting($this->getDoctrine()->getManager()));
        return $this->comLists($tools);
    }


    /**
     * @route("Question/tools",name="QuestionTools")
     */
    public function tools(Request $request)
    {
        $this->setSetting(new QuestionSetting($this->getDoctrine()->getManager()));
        return $this->comTools($request);
    }

}
